package soccerscope.db;

import java.io.*;
import java.util.Properties;

/**
 * <p>
 * Title:
 * </p>
 * 
 * <p>
 * Description:
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2006
 * </p>
 * 
 * <p>
 * Company:
 * </p>
 * 
 * @author not attributable
 * @version 1.0
 */
public class DatabaseConfig {

  private String                sqlHost;
  private String                sqlDB;
  private String                sqlUser;
  private String                sqlPassword;
  private Properties            prop     = new Properties();
  private FileInputStream       configFile;

  private static DatabaseConfig dbConfig = null;

  public DatabaseConfig() {

    try {
      this.configFile = new FileInputStream("../database.config");
    }
    catch (FileNotFoundException ex1) {
      ex1.printStackTrace();
    }
  }

  public void load() {

    try {
      this.prop.load(this.configFile);
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
    this.sqlHost = this.prop.getProperty("Host");
    this.sqlDB = this.prop.getProperty("DB");
    this.sqlUser = this.prop.getProperty("User");
    this.sqlPassword = this.prop.getProperty("Password");
    this.setDetectPlaying(this.prop.getProperty("DetectPlaying"));
    this.setDetectTeams(this.prop.getProperty("DetectTeams"));
  }

  public void store() {

    try {
      this.prop.store(new FileOutputStream("../database.config"), null);
    }
    catch (IOException ex) {
      ex.printStackTrace();
    }
  }

  public static synchronized DatabaseConfig getInstance() {

    if (null == DatabaseConfig.dbConfig) {
      DatabaseConfig.dbConfig = new DatabaseConfig();
    }

    return DatabaseConfig.dbConfig;
  }

  /**
   * @return Returns the sqlDB.
   */
  public String getSqlDB() {

    return this.sqlDB;
  }

  /**
   * @param sqlDB
   *          The sqlDB to set.
   */
  public void setSqlDB(String sqlDB) {

    this.sqlDB = sqlDB;
    this.prop.setProperty("DB", sqlDB);
  }

  /**
   * @return Returns the sqlHost.
   */
  public String getSqlHost() {

    return this.sqlHost;
  }

  /**
   * @param sqlHost
   *          The sqlHost to set.
   */
  public void setSqlHost(String sqlHost) {

    this.sqlHost = sqlHost;
    this.prop.setProperty("Host", sqlHost);
  }

  /**
   * @return Returns the sqlPassword.
   */
  public String getSqlPassword() {

    return this.sqlPassword;
  }

  /**
   * @param sqlPassword
   *          The sqlPassword to set.
   */
  public void setSqlPassword(String sqlPassword) {

    this.sqlPassword = sqlPassword;
    this.prop.setProperty("Password", sqlPassword);
  }

  public String getSqlUser() {

    return this.sqlUser;
  }

  public boolean isDetectTeams() {

    return (this.detectTeams.equalsIgnoreCase("true"));
  }

  public boolean isDetectPlaying() {

    return (this.detectPlaying.equalsIgnoreCase("true"));
  }

  public void setSqlUser(String sqlUser) {

    this.sqlUser = sqlUser;
    this.prop.setProperty("User", sqlUser);
  }

  public void setDetectTeams(String detectTeams) {

    this.detectTeams = detectTeams;
    this.prop.setProperty("DetectTeams", detectTeams);
  }

  public void setDetectPlaying(String detectPlaying) {

    this.detectPlaying = detectPlaying;
    this.prop.setProperty("DetectPlaying", detectPlaying);
  }

  private String detectTeams;  // Szene werden nur gespeichert wenn beide Teams
                                // geladen werden
  private String detectPlaying; // Szene werden nur gespeichert nachdem es
                                // gespielt wird
}
